// 云函数入口文件
const cloud = require('wx-server-sdk')
const env = 'bbq-6gz7knah1c296b0f'
cloud.init(env)

const db = cloud.database({ env })

// 云函数入口函数
exports.main = async (event, context) => {
  console.log(event);
  var data = {}
  await db.collection('articles').where({
    authorId:event.userId
  }).get().then(res=>{
    data.articles = res.data
    // console.log(res.data);
  })
  await db.collection('comments').where({
    commentUserId:event.userId
  }).get().then(res=>{
    // console.log(res.data);
    data.comments = res.data
  })
  await db.collection('users').where({
    userId:event.userId
  }).get().then(res=>{
    console.log(res.data);
    data.stores = res.data[0].stores,
    data.likes = res.data[0].likes
  })
  await db.collection('articles').aggregate()
  .lookup({
    from:'users',
    localField:'authorId',
    foreignField:'userId',
    as:'articletWithuser'
  }).match({
    _id:db.command.in(data.stores)
  }).end().then(res=>{
    console.log(res,111);
    data.stores = res.list
  })

  await db.collection('articles').aggregate()
  .lookup({
    from:'users',
    localField:'authorId',
    foreignField:'userId',
    as:'articletWithuser'
  }).match({
    _id:db.command.in(data.likes)
  }).end().then(res=>{
    data.likes = res.list
  })

  return {
    data
  }
}